package com.fuluwa.interfaces.bank.web;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fuluwa.domain.vendor.model.Vendor;
import com.fuluwa.infra.context.UserUtils;
import com.fuluwa.infra.exception.PersistenceException;
import com.fuluwa.infra.web.AbstractDomainController;
import com.fuluwa.infra.web.Result;
import com.fuluwa.interfaces.bank.facade.BankServiceFacade;
import com.fuluwa.interfaces.bank.facade.ResponseDTO;

@Controller
@RequestMapping(value = "/bank")
public class BankController extends AbstractDomainController {

	@Autowired
	BankServiceFacade bankServiceFacade;

	@RequestMapping(value = "preDeposit", method = RequestMethod.POST)
	@ResponseBody
	public Result preDeposit(@RequestParam BigDecimal amount, @RequestParam String vendor, @RequestParam String desc) {
		logger.debug("request deposit(POST):" + vendor);
		String accountNo = bankServiceFacade.getAccountNo(UserUtils.getCurrentUserName());
		try {
			ResponseDTO result = bankServiceFacade.prepareDeposit(accountNo, amount, Vendor.valueOf(vendor), desc);
			return getSuccessResult(result);
		} catch (Exception e) {
			logger.error("error", e);
			return getFailResult("Deposit Failed:" + e.getMessage());
		}
	}

	@RequestMapping(value = "depositCallback", method = RequestMethod.GET)
	@ResponseBody
	public Result deposit(@RequestParam String referenceNo, @RequestParam String vendor,
			@RequestParam String replyContent) {
		logger.debug("request deposit(GET):" + referenceNo);
		try {
			bankServiceFacade.doDeposit(referenceNo, replyContent, Vendor.valueOf(vendor));
			return getSuccessResult();
		} catch (Exception e) {
			logger.error("error", e);
			return getFailResult("Deposit Failed:" + e.getMessage());
		}
	}

	@RequestMapping(value = "preWithdrawl", method = RequestMethod.POST)
	@ResponseBody
	public Result preWithdrawl(@RequestParam BigDecimal amount, @RequestParam String vendor, @RequestParam String desc) {
		logger.debug("request deposit(POST):" + vendor);
		String accountNo = bankServiceFacade.getAccountNo(UserUtils.getCurrentUserName());
		try {
			ResponseDTO result = bankServiceFacade.prepareWithdrawl(accountNo, amount, Vendor.valueOf(vendor), desc);
			return getSuccessResult(result);
		} catch (Exception e) {
			logger.error("error", e);
			return getFailResult("Deposit Failed:" + e.getMessage());
		}
	}

	@RequestMapping(value = "withdrawlCallback", method = RequestMethod.GET)
	@ResponseBody
	public Result withdrawl(@RequestParam String referenceNo, @RequestParam String vendor,
			@RequestParam String replyContent) {
		logger.debug("request deposit(GET):" + referenceNo);
		try {
			bankServiceFacade.doWithdrawl(referenceNo, replyContent, Vendor.valueOf(vendor));
			return getSuccessResult();
		} catch (Exception e) {
			logger.error("error", e);
			return getFailResult("Deposit Failed:" + e.getMessage());
		}
	}
}
